

'template which can be cloned into the world.
Type TEntityTemplate

	Field _templateID:Int

	'string tmap holding component settings, value is also string.
	Field _componentSettings:TMap
    
    'friendly label rendered in editor gui
    Field _editorLabel:String
	
	
	Method New()
		_componentSettings = New TMap
	End Method
	
	
	
	Method AddComponentSetting(label:String, settings:String)
	
		'label' is not a component, but an editor friendly name
		'check it before other settings	
        If label = "label"
			_editorLabel = settings
            Return
        EndIf
		
		_componentSettings.Insert(label, settings)
	End Method
	
	
	
	'returns the component setting as a string
	Method GetComponentSetting:String(label:String)
		Return String(_componentSettings.ValueForKey(label))
	End Method
    
	
	
	Method SetTemplateID(newID:Int)
		_templateID = newID
	End Method
	
	
	
	Method GetTemplateId:Int()
		Return _templateID
	End Method
	
	
    
    Method GetLabel:string()
        Return _editorLabel
    End Method
	
	
	
	Method Write(s:TStream)
		Local settings:String
		For Local valueKey:String = EachIn _componentSettings.Keys()
			settings = Self.GetComponentSetting(valueKey)
			WriteLine(s, valueKey + "=" + settings)
		Next
	End Method
	
	
	
	rem
	bbdoc: Clones this template to an entity and puts it into the artemax world.
	endrem	
	Method CloneToEntity:TEntity(w:TWorld, renderLayer:Int, templateSetId:Int)

		Local e:TEntity = w.CreateEntity()
		
		'store templateset id and template id
		'needed when we save the world later on
		e.AddComponent(TTemplateComponent.Create(templateSetId, _templateID))
		
		If _componentSettings.Contains("script")
			Local setting:String = String(_componentSettings.ValueForKey("script"))
			e.AddComponent(TScriptComponent.Create(setting))
		End If
		
		If _componentSettings.Contains("color")
			Local array:String = String(_componentSettings.ValueForKey("color"))
			Local vals:String[] = array.split(",")
			Local r:Int = Int(vals[0])
			Local g:Int = Int(vals[1])
			Local b:Int = Int(vals[2])
			Local a:Float = Float(vals[3])
			e.AddComponent(TColorComponent.Create(r, g, b, a))
		Else
			'MUST have color component
			e.AddComponent(TColorComponent.Create(255, 255, 255, 1.0))
		End If
		
		If _componentSettings.Contains("transform")
			Local array:String = String(_componentSettings.ValueForKey("transform"))
			Local vals:String[] = array.split(",")
			Local x:Float = Float(vals[0])
			Local y:Float = Float(vals[1])
			Local rot:Float = Float(vals[2])
			e.AddComponent(TTransformComponent.Create(x, y, rot))
		Else
			'MUST have transform component
			e.AddComponent(TTransformComponent.Create(1.0, 1.0, 0.0))
		End If
		
		If _componentSettings.Contains("position")
			Local array:String = String(_componentSettings.ValueForKey("position"))
			Local vals:String[] = array.split(",")
			Local x:Float = Float(vals[0])
			Local y:Float = Float(vals[1])
			e.AddComponent(TPositionComponent.Create(x, y))
		Else
			'MUST have position component
			e.AddComponent(TPositionComponent.Create(0, 0))
		EndIf

		If _componentSettings.Contains("physics")
			Local setting:String = String(_componentSettings.ValueForKey("physics"))
			e.AddComponent(TPhysicsComponent.Create(Float(setting)))
		End If

		If _componentSettings.Contains("hitbox")
			Local array:String = String(_componentSettings.ValueForKey("hitbox"))
			Local vals:String[] = array.split(",")
			Local x:Float = Float(vals[0])
			Local y:Float = Float(vals[1])
			Local collision:Int = Int(vals[2])
			e.AddComponent(THitBoxComponent.Create(x, y, collision))
		Else
			'MUST have hitbox component.
			e.AddComponent(THitBoxComponent.Create(32, 32, True))
		End If

		If _componentSettings.Contains("renderform")
			Local array:String = String(_componentSettings.ValueForKey("renderform"))
			Local vals:String[] = array.split(",")
			Local formname:String = vals[0]
			Local resourcesGroupName:String = vals[1]
			Local frameNumber:Int = Int(vals[2])
			e.AddComponent(TRenderFormComponent.Create(formname, resourcesGroupName, frameNumber, renderLayer))
		End If
		
		'MUST have quadtree component.
		e.AddComponent(New TQuadTreeComponent)

		e.Refresh()
		Return e
	End Method

End Type